package com.ltu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltu.config.shiro.dto.UserDto;
import com.ltu.domain.mp_entity.RoleMenu;
import com.ltu.mapper.RoleMenuMapper;
import com.ltu.model.request.account.RoleMenuReq;
import com.ltu.model.request.base.BaseIdReq;
import com.ltu.model.request.base.PageReqData;
import com.ltu.model.response.MenuResp;
import com.ltu.model.response.base.CodeDataResp;
import com.ltu.service.AccountService;
import com.ltu.service.RoleMenuService;
import com.ltu.util.MenuUtil;
import com.ltu.util.common.IdsStrToListUtils;
import com.ltu.util.common.StrUtils;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Vescky.L
 * @since 2019-10-24
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

//    private final AccountService accountService;

    @Override
    public CodeDataResp updateRoleMenu(RoleMenuReq roleMenuReq) {
        //清除旧记录
        QueryWrapper<RoleMenu> condition = new QueryWrapper<>();
        condition.eq("role_id",roleMenuReq.getRoleId());
        super.remove(condition);
        //插入新记录
        List<String> menuIdList = IdsStrToListUtils.getStringList(roleMenuReq.getMenuIds());
        List<RoleMenu> roleMenuList = new ArrayList<>();
        for (String menuId : menuIdList) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId)
                    .setRoleId(roleMenuReq.getRoleId());
            roleMenuList.add(roleMenu);
        }
        super.saveBatch(roleMenuList);

        return CodeDataResp.valueOfSuccessEmptyData();
    }

	@Override
	public CodeDataResp getRoleMenu(BaseIdReq baseIdReq) {
		// TODO Auto-generated method stub
		return null;
	}

//    @Override
//    public CodeDataResp getMenuByAccount(PageReqData baseAccountReqData) {
//        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
//        AccountEntity account = accountService.selectById(userDto.getId());
//        RoleMenu roleMenu = getMenuListByRoleId(account.getRoleId());
//        if (roleMenu==null)
//           return CodeDataResp.valueOfFailed("该角色暂无登录权限");
//
//        return CodeDataResp.valueOfSuccess(roleMenu);
//    }


}
